package com.jewaton.auth.commons.dao;

import com.jewaton.auth.commons.entity.District;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("districtDAO")
public class DistrictDAO extends BaseDAO {

	
	@SuppressWarnings("unchecked")
	public List<District> getAll(){
		String hql = "from District where parentId =:parentId and isCommon =:isCommon";
		Query query = this.getSession().createQuery(hql); 
		query.setString("parentId", "0001");
		query.setInteger("isCommon", 1);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<District> getAllDistrict(){
		String hql = "from District where did !='0001' order by dcode asc";
		Query query = this.getSession().createQuery(hql); 
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public District getByDistrictName(String districtName){
		District district = null;
		String hql = "select d from District d where d.districtName = :districtName";
		Query query = this.getSession().createQuery(hql); 
		query.setString("districtName", districtName);
		List<District> list = query.list();
		if(!list.isEmpty()){
			district = list.get(0);
		}
		return district;
	}
}
